package cn.itsource.test.sand.demo;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DateRangeExample {
    public static void main(String[] args) {
        // 定义初始时间
        LocalDateTime startDateTime = LocalDateTime.of(2023, 6, 13, 23, 59, 59);
        LocalDateTime endDateTime = LocalDateTime.of(2023, 6, 14, 0, 0, 0);

        // 获取一年前的时间
        LocalDateTime oneYearStart = startDateTime.minusYears(1);
        LocalDateTime oneYearEnd = endDateTime.minusYears(1);

        // 输出一年前的时间段
        System.out.println(formatDateTime(oneYearStart) + " - " + formatDateTime(startDateTime));

        // 逐天增加时间并输出时间段，直到达到最初给定的时间
        while (!oneYearStart.equals(startDateTime) || !oneYearEnd.equals(endDateTime)) {
            oneYearStart = oneYearStart.plusDays(1);
            oneYearEnd = oneYearEnd.plusDays(1);
            System.out.println(formatDateTime(oneYearStart) + " - " + formatDateTime(oneYearEnd));
        }
    }

    private static String formatDateTime(LocalDateTime dateTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return dateTime.format(formatter);
    }
}
